// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import mainManage from "../../framework/mainManage";
import Tool from "../../framework/Tool";
import gameData from "../gameData";


const {ccclass, property} = cc._decorator;

export enum directionList {
    Horizontal = 0,
    Vertical = 1
}

@ccclass
export default class petViewMediator extends cc.Component {

    @property({visible : false})
    Direction : directionList = directionList.Horizontal;

    @property({type : cc.Enum(directionList),displayName : "模式"})
    get updateDirection() : directionList{
        return this.Direction;
    }
    set updateDirection(value){
        if (this.Direction === value) {
            return;
        }
        this.Direction = value;
        this._initItemPos();
    }

    @property({visible : false})
    itemOffset : number = 0;
    @property({displayName : "每个item间隔"})
    get positionGet(): number{
        return this.itemOffset;
    }
    set positionGet(value: number){
        if (this.itemOffset === value) {
            return;
        }
        this.itemOffset = value;
        this._initItemPos();
    }

    @property({visible : false})
    speed : number = 0;

    @property({displayName : "移动速度"})
    get speedGet(): number{
        return this.speed;
    }
    set speedGet(value: number){
        if (this.speed === value) {
            return;
        }
        this.speed = value;
        this._initItemPos();
    }


    @property
    item : cc.Node[] = [];

    @property({type : cc.Node,displayName : "滚动item"})
    get itemGet(){
        return this.item;
    }
    set itemGet(value){
        this._initItemPos();
    }

    @property({displayName : "减速频率"})
    rub : number = 1.0;

    @property({displayName : "缩放最小值"})
    scaleMin : number = 0.5;

    @property({displayName : "缩放最大值"})
    scaleMax : number = 1;

    @property({type : cc.Node,displayName : "按钮节点"})
    btnNode : cc.Node = null;

    @property(cc.Label)
    totalNumber : cc.Label = null;

    @property(cc.Node)
    eggBtn : cc.Node = null;

    @property(cc.Node)
    videoBtn : cc.Node = null;

    @property(cc.Label)
    skinPiece : cc.Label = null;

    @property(cc.Node)
    exchangeNode : cc.Node = null;

    @property(cc.Node)
    seeVideoBtn : cc.Node = null;

    @property(cc.Node)
    rightList : cc.Node[] = [];

    @property(cc.Node)
    getTimeLab : cc.Node[] = [];

    @property(cc.Node)
    pieceList : cc.Node = null;

    _startTime = 0;

    _moveSpeed = 0;

    _maxSize = null;

    _screenRect = null;

    clickStatus = [];

    itemList = [];

    _initItemPos() {
        this.node.anchorY = 0.5;
        this.node.anchorX = 0.5;
        this._maxSize = new cc.Size(0, 0);
        for (let i = 0; i < this.item.length; i++) {
            this._maxSize.width += this.item[i].width;
            this._maxSize.height += this.item[i].height;
            this._maxSize.width += this.itemOffset;
            this._maxSize.height += this.itemOffset;
        }
        let startPos;
        if (this.Direction == 0) {
            // startPos = cc.v2(-this._maxSize.width * this.node.anchorX, -this._maxSize.height * this.node.anchorY);
            startPos = cc.v2(0, -this._maxSize.height * this.node.anchorY);
        } else {
            startPos = cc.v2(this._maxSize.width * this.node.anchorX, this._maxSize.height * this.node.anchorY / 2);
        }
        this._screenRect = new cc.Rect(startPos.x, startPos.y, this._maxSize.width, this._maxSize.height);
        for (let i = 0; i < this.item.length; i++) {
            let anchor = this.item[i].getAnchorPoint();
            let itemSize = this.item[i].getContentSize();

            if (this.Direction == 0) {
                startPos.addSelf(cc.v2(itemSize.width * anchor.x, itemSize.height * anchor.y));
                this.item[i].x = startPos.x;
                this.item[i].y = 0;
                startPos.addSelf(cc.v2(itemSize.width * anchor.x, itemSize.height * anchor.y));
                startPos.addSelf(cc.v2(this.itemOffset, this.itemOffset));
            } else {
                startPos.subSelf(cc.v2(itemSize.width * anchor.x, itemSize.height * anchor.y));
                this.item[i].x = 0;
                this.item[i].y = startPos.y;
                startPos.subSelf(cc.v2(itemSize.width * anchor.x, itemSize.height * anchor.y));
                startPos.subSelf(cc.v2(this.itemOffset, this.itemOffset));
            }
            this.itemList[i] = this.item[i];
        }
    }

    useData = [];

    onLoad(){

        //新年相关
        this.clickStatus = gameData.newYearPetSelectStatus;
        for(let i = 0; i < this.clickStatus.length ;i++) {
            if (gameData.getPetProcessSkin()[i] == 1) {
                if(gameData.newYearPetLockStatus[i] == 0){
                    this.clickStatus[i] = 0;
                    gameData.newYearPetSelectStatus[i] = 0;
                }else{
                    if(gameData.curUseSkin.indexOf(i) > -1 && gameData.newYearPetUseStatus[i] == 0){
                        this.clickStatus[i] = 0;
                        gameData.newYearPetSelectStatus[i] = 0;
                    }
                }
            } else {
                if(gameData.newYearPetLockStatus[i] == 1 || gameData.newYearPetUseStatus[i] == 1){
                    this.clickStatus[i] = 1;
                    gameData.newYearPetSelectStatus[i] = 1;
                }
            }
        }

        gameData.saveOtherGameData();

        if(gameData.colorfulPiece[1][3] == 0){
            this.item[9].getComponent(cc.Button).enabled = true;
        }else{
            this.item[9].getComponent(cc.Button).enabled = false;
        }

        this.skinPiece.string = gameData.petPiece + "";
        this._initItemPos();
        this.updateScale();
        let levelData = gameData.getPetShopData();
        let processData = gameData.getPetProcessSkin();
        this.useData = gameData.getPetCurSkin();
        let index = 0;
        for (let i = 0; i < this.item.length; i++) {
            let spk = this.item[i].getChildByName("demoMan").getComponent(sp.Skeleton);
            if(gameData.newYearPetUseStatus[i] == 1){
                spk.setSkin("pf02");
            }else{
                if(this.clickStatus[i] == 0){
                    spk.setSkin("pf01");
                }else{
                    spk.setSkin("pf02")
                }
            }

            let name = this.item[i].getChildByName("name");
            name.color = cc.color(255,255,255);
            name.scale = 0.5;
            name.width = 300;
            name.getComponent(cc.Label).string = levelData[i]["psname"];
            name.getComponent(cc.Label).overflow = cc.Label.Overflow.RESIZE_HEIGHT;
            let outLine = name.addComponent(cc.LabelOutline);
            outLine.color = cc.color(0,0,0);
            outLine.width = 4;

            if(processData[i] == 1){
                index++;
                this.item[i].getChildByName("lock").active = false;
                if(i == 0){
                    if(gameData.newYearPetSelectStatus[0] == 0){
                        this.btnNode.getChildByName("bg3").active = false;
                        this.btnNode.getChildByName("bg1").active = true;
                    }else{
                        for(let i in this.getTimeLab){
                            this.getTimeLab[i].active = false;
                        }
                        this.seeVideoBtn.active = false;
                        this.exchangeNode.active = false;
                        this.eggBtn.active = false;
                        this.videoBtn.active = false;
                        if(gameData.newYearPetLockStatus[index] == 0) {
                            this.item[index].getChildByName("lock").active = true;
                            this.item[index].getChildByName("use").active = false;
                            this.seeVideoBtn.active = true;
                            //显示宠物装扮lab
                            gameData.judgeTimeCome(10,()=>{
                                this.getTimeLab[0].active = false;
                            },()=>{
                                this.getTimeLab[0].active = true;
                            });
                        }else{
                            this.item[index].getChildByName("lock").active = false;
                            if(gameData.newYearPetUseStatus[index] == 0){
                                this.item[index].getChildByName("use").active = false;
                                this.btnNode.getChildByName("bg1").active = true;
                                this.btnNode.getChildByName("bg2").active = false;
                                this.btnNode.getChildByName("bg3").active = false;
                            }else{
                                this.item[index].getChildByName("use").active = true;
                                this.btnNode.getChildByName("bg1").active = false;
                                this.btnNode.getChildByName("bg2").active = true;
                                this.btnNode.getChildByName("bg3").active = false;
                            }
                            this.seeVideoBtn.active = false;
                        }
                    }
                }
            }else{
                if(i == 0){
                    if(gameData.newYearPetSelectStatus[0] == 0){
                        this.btnNode.getChildByName("bg3").active = true;
                    }else{
                        for(let i in this.getTimeLab){
                            this.getTimeLab[i].active = false;
                        }
                        this.seeVideoBtn.active = false;
                        this.exchangeNode.active = false;
                        this.eggBtn.active = false;
                        this.videoBtn.active = false;
                        if(gameData.newYearPetLockStatus[index] == 0) {
                            this.item[index].getChildByName("lock").active = true;
                            this.item[index].getChildByName("use").active = false;
                            this.seeVideoBtn.active = true;

                            //显示宠物装扮lab
                            gameData.judgeTimeCome(10,()=>{
                                this.getTimeLab[0].active = false;
                            },()=>{
                                this.getTimeLab[0].active = true;
                            });
                        }else{
                            this.item[index].getChildByName("lock").active = false;
                            if(gameData.newYearPetUseStatus[index] == 0){
                                this.item[index].getChildByName("use").active = false;
                                this.btnNode.getChildByName("bg1").active = true;
                                this.btnNode.getChildByName("bg2").active = false;
                                this.btnNode.getChildByName("bg3").active = false;
                            }else{
                                this.item[index].getChildByName("use").active = true;
                                this.btnNode.getChildByName("bg1").active = false;
                                this.btnNode.getChildByName("bg2").active = true;
                                this.btnNode.getChildByName("bg3").active = false;
                            }
                            this.seeVideoBtn.active = false;
                        }
                    }
                }
            }

            if(this.useData.indexOf(i) > -1){
                this.item[i].getChildByName("use").active = true;
                if(i == 0){
                    this.btnNode.getChildByName("bg2").active = true;
                }
            }
        }

        if(gameData.newYearPetLockStatus[0] == 1){
            this.seeVideoBtn.active = false;
        }else{
            this.seeVideoBtn.active = true;
        }

        let spk = this.item[0].getChildByName("demoMan").getComponent(sp.Skeleton);
        if(this.clickStatus[0] == 0){
            spk.setSkin("pf01");
            this.seeVideoBtn.active = false;
            this.rightList[0].active = true;

            this.rightList[1].active = false;
        }else{
            spk.setSkin("pf02");
            this.rightList[0].active = false;
            this.rightList[1].active = true;
        }
        this.totalNumber.string = index + "," + this.item.length;
        this.node.on('touchstart', function (event) {
            this._moveSpeed = 0;
            this._startTime = new Date().getTime();
        }.bind(this));

        this.node.on('touchmove', function (event) {
            let movePos = event.getDelta();
            this.itemMoveBy(movePos);
        }.bind(this));

        this.node.on('touchend', function (event) {
            this.touchEnd(event)
        }.bind(this));

        this.node.on('touchcancel', function (event) {
            this.touchEnd(event)
        }.bind(this));
    }

    touchEnd(event) {

        let curpos = event.getLocation();
        let startpos = event.getStartLocation();

        let dis;
        if (this.Direction == 0) {
            dis = startpos.x - curpos.x;

        } else {
            dis = startpos.y - curpos.y;
        }

        let curTime = new Date().getTime();
        let disTime = curTime - this._startTime;
        this._moveSpeed = dis / disTime;
    }

    itemMoveBy(pos) {
        let startItem = this.itemList[0];
        let endItem = this.itemList[this.itemList.length - 1];
        if(startItem.x + pos.x >= 0){
            pos.x = -startItem.x;
        }else if(endItem.x + pos.x <= 0){
            pos.x = -endItem.x;
        }
        for (let i = 0; i < this.item.length; i++) {
            if (this.Direction == 0) {
                this.item[i].x += pos.x;
            } else {
                this.item[i].y += pos.y;
            }
        }
        this.updatePos();
    }

    updatePos() {
        this.updateScale();
    }

    lastUseSkin = 0;
    changeSkinBtn(event,data){
        let arr  = [];
        for(let i in this.item){
            arr.push(this.item[i].scale);
        }
        let index = this.getMaxIndex(arr);
        let spk = this.item[index].getChildByName("demoMan").getComponent(sp.Skeleton);
        this.rightList[this.clickStatus[index]].active = false;
        this.rightList[parseInt(data) - 1].active = true;
        this.clickStatus[index] = parseInt(data) - 1;
        gameData.newYearPetSelectStatus[index] =  parseInt(data) - 1;
        gameData.saveOtherGameData();
        if(parseInt(data) == 1){
            this.seeVideoBtn.active = false;
            for(let i in this.getTimeLab){
                this.getTimeLab[i].active = false;
            }
            spk.setSkin("pf01");
            if(gameData.getPetProcessSkin()[index] == 0){

                if(index == 27){
                    //时间显示宠物小抖
                    gameData.judgeTimeCome(15,()=>{
                        this.getTimeLab[1].active = false;
                    },()=>{
                        this.getTimeLab[1].active = true;
                    });
                }
                if (index == 24) {
                    this.exchangeNode.active = false;
                    if (gameData.getRoleProcessSkin()[20] == 1) {
                        this.eggBtn.active = false;
                        this.videoBtn.active = true;
                    } else {
                        this.eggBtn.active = true;
                        this.videoBtn.active = false;
                    }
                } else if ((index > 18 && index < 24) || index == 27) {
                    this.eggBtn.active = false;
                    this.videoBtn.active = false;
                    this.exchangeNode.active = true;
                } else if (index == 18) {
                    this.exchangeNode.active = false;
                    if (gameData.getRoleProcessSkin()[17] == 1) {
                        this.eggBtn.active = false;
                        this.videoBtn.active = true;
                    } else {
                        this.eggBtn.active = true;
                        this.videoBtn.active = false;
                    }
                } else if(index == 26){
                    this.exchangeNode.active = false;

                    //熊猫崽崽
                    gameData.judgeTimeCome(14,()=>{
                        this.getTimeLab[2].active = false;
                        if(gameData.colorfulPiece[9][0] == 1){
                            this.eggBtn.active = false;
                            this.videoBtn.active = true;
                        }else{
                            this.eggBtn.active = true;
                            this.videoBtn.active = false;
                        }
                    },()=>{
                        this.getTimeLab[2].active = true;
                        this.eggBtn.active = true;
                        this.videoBtn.active = false;
                    });

                }else if(index == 28){
                    this.exchangeNode.active = false;

                    //舞狮
                    gameData.judgeTimeCome(12,()=>{
                        this.getTimeLab[3].active = false;
                        if(gameData.colorfulPiece[7][0] == 1){
                            this.eggBtn.active = false;
                            this.videoBtn.active = true;
                        }else{
                            this.eggBtn.active = true;
                            this.videoBtn.active = false;
                        }
                    },()=>{
                        this.getTimeLab[3].active = true;
                        this.eggBtn.active = true;
                        this.videoBtn.active = false;
                    });
                }else if(index == 29){
                    this.exchangeNode.active = false;

                    //福星小牛
                    gameData.judgeTimeCome(13,()=>{
                        this.getTimeLab[4].active = false;
                        if(gameData.colorfulPiece[8][0] == 1){
                            this.eggBtn.active = false;
                            this.videoBtn.active = true;
                        }else{
                            this.eggBtn.active = true;
                            this.videoBtn.active = false;
                        }
                    },()=>{
                        this.getTimeLab[4].active = true;
                        this.eggBtn.active = true;
                        this.videoBtn.active = false;
                    });
                }else{
                    this.exchangeNode.active = false;
                    this.eggBtn.active = false;
                    this.videoBtn.active = false;
                }
                this.item[index].getChildByName("lock").active = true;
                this.item[index].getChildByName("use").active = false;
                this.btnNode.getChildByName("bg1").active = false;
                this.btnNode.getChildByName("bg2").active = false;
                this.btnNode.getChildByName("bg3").active = true;
            }else{
                this.eggBtn.active = false;
                this.videoBtn.active = false;
                this.exchangeNode.active = false;
                if (this.useData.indexOf(index) > -1 && gameData.newYearPetUseStatus[index] == 0) {
                    this.item[index].getChildByName("lock").active = false;
                    this.item[index].getChildByName("use").active = true;
                    this.btnNode.getChildByName("bg1").active = false;
                    this.btnNode.getChildByName("bg2").active = true;
                    this.btnNode.getChildByName("bg3").active = false;
                } else {
                    this.item[index].getChildByName("lock").active = false;
                    this.item[index].getChildByName("use").active = false;
                    this.btnNode.getChildByName("bg1").active = true;
                    this.btnNode.getChildByName("bg2").active = false;
                    this.btnNode.getChildByName("bg3").active = false;
                }
            }
        }else if(parseInt(data) == 2){
            for(let i in this.getTimeLab){
                this.getTimeLab[i].active = false;
            }
            this.seeVideoBtn.active = false;
            this.exchangeNode.active = false;
            this.eggBtn.active = false;
            this.videoBtn.active = false;
            spk.setSkin("pf02");
            if(gameData.newYearPetLockStatus[index] == 0) {
                this.item[index].getChildByName("lock").active = true;
                this.item[index].getChildByName("use").active = false;
                this.seeVideoBtn.active = true;

                //显示宠物装扮lab
                gameData.judgeTimeCome(10,()=>{
                    this.getTimeLab[0].active = false;
                },()=>{
                    this.getTimeLab[0].active = true;
                });
            }else{
                this.item[index].getChildByName("lock").active = false;
                if(gameData.newYearPetUseStatus[index] == 0){
                    this.item[index].getChildByName("use").active = false;
                    this.btnNode.getChildByName("bg1").active = true;
                    this.btnNode.getChildByName("bg2").active = false;
                    this.btnNode.getChildByName("bg3").active = false;
                }else{
                    this.item[index].getChildByName("use").active = true;
                    this.btnNode.getChildByName("bg1").active = false;
                    this.btnNode.getChildByName("bg2").active = true;
                    this.btnNode.getChildByName("bg3").active = false;
                }
                this.seeVideoBtn.active = false;
            }
        }
    }

    seeVideoGetNewYearSkin(){
        gameData.judgeTimeCome(10,()=>{
            let arr  = [];
            for(let i in this.item){
                arr.push(this.item[i].scale);
            }
            let index = this.getMaxIndex(arr);
            this.seeVideoBtn.active = false;
            gameData.newYearPetLockStatus[index] = 1;

            this.item[index].getChildByName("lock").active = false;
            this.btnNode.getChildByName("bg1").active = true;
            this.btnNode.getChildByName("bg2").active = false;
            this.btnNode.getChildByName("bg3").active = false;
            gameData.saveOtherGameData();
            mainManage.createToast("恭喜获得春节皮肤");

        },()=>{
            mainManage.createToast("2月10日后可获取!");
        });
    }


    btnClick(event,data){
        if(Math.abs(this._moveSpeed) > 1) {
            //mainManage.createToast("请选择好宠物");
            return;
        }
        let arr  = [];
        for(let i in this.item){
            arr.push(this.item[i].scale);
        }
        let index = this.getMaxIndex(arr);
        let date = gameData.judgeWearPetNumber(gameData.curRankTitle);
        // let date = 2;
        if(gameData.getPetProcessSkin()[index] == 1){
            if(this.clickStatus[index] == 0){
                if(gameData.newYearPetUseStatus[index] == 1){
                    gameData.newYearPetUseStatus[index] = 0;
                    gameData.saveOtherGameData();
                    this.btnNode.getChildByName("bg1").active = false;
                    this.btnNode.getChildByName("bg2").active = true;
                    this.btnNode.getChildByName("bg3").active = false;
                    this.item[index].getChildByName("use").active = true;
                    cc.game.emit("resetPet");
                }else{
                    if(this.useData.length >= date && this.useData.indexOf(index) < 0){
                        mainManage.createToast("Hanya bisa membawa satu hewan peliharaan");
                    }else if(this.useData.indexOf(index) < 0 && this.useData.length  < date){
                        this.useData.push(index);
                        this.btnNode.getChildByName("bg1").active = false;
                        this.btnNode.getChildByName("bg2").active = true;
                        this.btnNode.getChildByName("bg3").active = false;
                        this.item[index].getChildByName("use").active = true;
                        gameData.setPetCurSkin(this.useData);
                        gameData.saveLastPetSkin();
                        cc.game.emit("resetPet");
                        gameData.newYearPetUseStatus[index] = 0;
                        gameData.saveOtherGameData();
                    }else if(this.useData.indexOf(index) > -1){
                        this.useData.splice(this.useData.indexOf(index),1);
                        this.item[index].getChildByName("use").active = false;
                        this.btnNode.getChildByName("bg1").active = true;
                        this.btnNode.getChildByName("bg2").active = false;
                        this.btnNode.getChildByName("bg3").active = false;
                        gameData.setPetCurSkin(this.useData);
                        gameData.saveLastPetSkin();
                        cc.game.emit("resetPet");
                        gameData.newYearPetUseStatus[index] = 0;
                        gameData.saveOtherGameData();
                    }
                }
            }else{
                if(gameData.newYearPetUseStatus[index] == 1){
                    gameData.newYearPetUseStatus[index] = 0;
                    this.item[index].getChildByName("use").active = false;
                    this.btnNode.getChildByName("bg1").active = true;
                    this.btnNode.getChildByName("bg2").active = false;
                    this.btnNode.getChildByName("bg3").active = false;
                    if(this.useData.indexOf(index) > -1){
                        this.useData.splice(this.useData.indexOf(index),1);
                        gameData.setPetCurSkin(this.useData);
                        gameData.saveLastPetSkin();
                        cc.game.emit("resetPet");
                    }
                }else{
                    if(this.useData.length >= date && this.useData.indexOf(index) < 0){
                        mainManage.createToast("Hanya bisa membawa satu hewan peliharaan");
                    }else{
                        gameData.newYearPetUseStatus[index] = 1;
                        gameData.saveOtherGameData();
                        if(this.useData.indexOf(index) < 0 && this.useData.length < date){
                            this.useData.push(index);
                            this.btnNode.getChildByName("bg1").active = false;
                            this.btnNode.getChildByName("bg2").active = true;
                            this.btnNode.getChildByName("bg3").active = false;
                            this.item[index].getChildByName("use").active = true;
                            gameData.setPetCurSkin(this.useData);
                            gameData.saveLastPetSkin();
                            cc.game.emit("resetPet");
                        } else if(this.useData.indexOf(index) > -1){
                            gameData.newYearPetUseStatus[index] = 0;
                            gameData.saveOtherGameData();
                            this.useData.splice(this.useData.indexOf(index),1);
                            gameData.setPetCurSkin(this.useData);
                            gameData.saveLastPetSkin();
                            cc.game.emit("resetPet");
                            this.item[index].getChildByName("use").active = false;
                            this.btnNode.getChildByName("bg1").active = true;
                            this.btnNode.getChildByName("bg2").active = false;
                            this.btnNode.getChildByName("bg3").active = false;
                        }
                    }
                }
                gameData.saveOtherGameData();
            }
        }else{
            if(this.clickStatus[index] == 1){
                if(gameData.newYearPetUseStatus[index] == 1){
                    gameData.newYearPetUseStatus[index] = 0;
                    if(this.useData.indexOf(index) > -1){
                        this.useData.splice(this.useData.indexOf(index),1);
                    }
                    gameData.saveOtherGameData();
                    this.item[index].getChildByName("use").active = false;
                    this.btnNode.getChildByName("bg1").active = true;
                    this.btnNode.getChildByName("bg2").active = false;
                    this.btnNode.getChildByName("bg3").active = false;
                    gameData.setPetCurSkin(this.useData);
                    gameData.saveLastPetSkin();
                    cc.game.emit("resetPet");
                }else{
                    if(this.useData.length >= date && this.useData.indexOf(index) < 0){
                        mainManage.createToast("Hanya bisa membawa satu hewan peliharaan");
                    }else {
                        gameData.newYearPetUseStatus[index] = 1;
                        gameData.saveOtherGameData();
                        if(this.useData.indexOf(index) < 0 && this.useData.length  < date){
                            this.useData.push(index);
                            this.btnNode.getChildByName("bg1").active = false;
                            this.btnNode.getChildByName("bg2").active = true;
                            this.btnNode.getChildByName("bg3").active = false;
                            this.item[index].getChildByName("use").active = true;
                            gameData.setPetCurSkin(this.useData);
                            gameData.saveLastPetSkin();
                            cc.game.emit("resetPet");
                        }
                    }
                }
            }else{
                if(index == 15){
                    mainManage.createToast("前往彩蛋墙查看解锁线索");
                }else{
                    mainManage.createToast("Belum Diperoleh");
                }
            }

        }
    }

    clickSkinTimes = 0;
    skinClick(event,data){
        if(this.clickSkinTimes < 0) return;
        this.clickSkinTimes++;
        if(this.clickSkinTimes >= 10){
            this.clickSkinTimes = -1;
            this.item[9].getChildByName("skin").active = true;
            let pos1 = this.node.convertToWorldSpaceAR(cc.v2(0,0));
            let pos2 = this.item[9].getChildByName("skin").convertToNodeSpace(pos1);
            cc.tween(this.item[9].getChildByName("skin")).by(0.5,{
                position : cc.v2(0,-cc.winSize.height / 2 + 100)
            }).call(()=>{
                cc.tween(this.item[9].getChildByName("skin")).to(0.5,{
                    position : pos2
                }).call(()=>{
                    this.scheduleOnce(()=>{
                        this.item[9].getChildByName("skin").active = false;
                    },1);
                    gameData.colorfulPiece[1][3] = 1;
                    gameData.saveColorfulPiece();
                    Tool.loadPrefab("prefabs/getPiece",res=>{
                        let view = cc.instantiate(res);
                        view.getComponent("getMaPiece").setCallBack(4);
                        view.parent = cc.find("Canvas");
                    })
                }).start();
            }).start();
        }
    }

    exchangePet(){
        let arr  = [];
        for(let i in this.item){
            arr.push(this.item[i].scale);
        }
        let index = this.getMaxIndex(arr);

        //时间显示宠物小抖
        if(index == 27){
            gameData.judgeTimeCome(15,()=>{
                if(gameData.getPetProcessSkin()[index] == 0){
                    if(gameData.petPiece < 150){
                        mainManage.createToast("宠物碎片不足，请前往幸运转盘获取碎片");
                    }else{
                        this.item[index].getChildByName("lock").active = false;
                        this.btnNode.getChildByName("bg3").active = false;
                        this.btnNode.getChildByName("bg2").active = false;
                        this.btnNode.getChildByName("bg1").active = true;
                        this.exchangeNode.active = false;
                        gameData.savePetSkin(index);
                        gameData.petPiece -= 150;
                        gameData.saveLotteryData();
                        this.skinPiece.string = gameData.petPiece + "";
                        mainManage.createToast("恭喜成功兑换");
                    }
                }
            },()=>{
                mainManage.createToast("2月15日开启兑换");
            });

        }
    }

    getMaxIndex(arr) {
        let max = arr[0];
        //声明了个变量 保存下标值
        let index = 0;
        for (let i = 0; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
                index = i;
            }
        }
        return index;
    }

    updateScale() {
        if (this.scaleMax < this.scaleMin || this.scaleMax == 0) {
            return;
        }
        for (let i = 0; i < this.item.length; i++) {
            let pre;
            if (this.Direction == 0) {
                let x = this.item[i].x + this._maxSize.width / 2;
                if (this.item[i].x < 0) {
                    pre = x / this._maxSize.width;
                }
                else {
                    pre = 1 - x / this._maxSize.width;
                }

            } else {
                let y = this.item[i].y + this._maxSize.height / 2;
                if (this.item[i].y < 0) {
                    pre = y / this._maxSize.height;
                }
                else {
                    pre = 1 - y / this._maxSize.height;
                }
            }
            pre *= 2;
            let scaleTo = this.scaleMax - this.scaleMin;
            scaleTo *= pre;
            scaleTo += this.scaleMin;
            scaleTo = Math.abs(scaleTo);
            this.item[i].scaleX = scaleTo;
            this.item[i].scaleY = scaleTo;
        }
    }


    eggSkinClick(){
        let arr  = [];
        for(let i in this.item){
            arr.push(this.item[i].scale);
        }
        let index = this.getMaxIndex(arr);
        if(index == 18){
            mainManage.createToast("解锁空空后即可获取");
        }else if(index == 24){
            mainManage.createToast("解锁阿七任意形态后可获得小飞鸡！");
        }else if(index == 26){
            gameData.judgeTimeCome(14,()=>{
                mainManage.createToast("找到熊猫崽崽信物后可获得！");
            },()=>{
                mainManage.createToast("2月14日开启兑换");
            });
        }else if(index == 28){
            gameData.judgeTimeCome(12,()=>{
                mainManage.createToast("找到舞狮信物后可获得！");
            },()=>{
                mainManage.createToast("2月12日开启兑换");
            });
        }else if(index == 29){
            gameData.judgeTimeCome(13,()=>{
                mainManage.createToast("找到福星小牛信物后可获得！");
            },()=>{
                mainManage.createToast("2月13日开启兑换");
            });
        }else if(index == 30){
            mainManage.createToast("温泉召唤术获取！");
        }
    }

    seeVideoGet(){
        let arr  = [];
        for(let i in this.item){
            arr.push(this.item[i].scale);
        }
        let index = this.getMaxIndex(arr);
        if(index == 18){

            this.item[index].getChildByName("lock").active = false;
            gameData.savePetSkin(18);
            this.videoBtn.active = false;
            this.btnNode.getChildByName("bg3").active = false;
            this.btnNode.getChildByName("bg1").active = true;

        }else if(index == 24){
            this.item[index].getChildByName("lock").active = false;
            gameData.savePetSkin(24);
            this.videoBtn.active = false;
            this.btnNode.getChildByName("bg3").active = false;
            this.btnNode.getChildByName("bg1").active = true;
        }else if(index == 28){
            this.item[index].getChildByName("lock").active = false;
            gameData.savePetSkin(28);
            this.videoBtn.active = false;
            this.btnNode.getChildByName("bg3").active = false;
            this.btnNode.getChildByName("bg1").active = true;
        }else if(index == 29){
            this.item[index].getChildByName("lock").active = false;
            gameData.savePetSkin(29);
            this.videoBtn.active = false;
            this.btnNode.getChildByName("bg3").active = false;
            this.btnNode.getChildByName("bg1").active = true;
        }else if(index == 26){
            this.item[index].getChildByName("lock").active = false;
            gameData.savePetSkin(26);
            this.videoBtn.active = false;
            this.btnNode.getChildByName("bg3").active = false;
            this.btnNode.getChildByName("bg1").active = true;
        }
    }

    callback = null;
    setCallBack(callback){
        this.callback = callback;
    }

    closeWindow(){
        this.node.destroy();
        if(this.callback != null){
            this.callback();
        }
    }

    isCheck = false;
    update(dt) {
        if (this._moveSpeed == 0) {
            if(!this.isCheck){
                this.isCheck = true;
                let arr  = [];
                for(let i in this.item){
                    arr.push(this.item[i].scale);
                }
                let index = this.getMaxIndex(arr);
                let pos = this.item[index].convertToWorldSpaceAR(cc.v2(0,0));
                let pos2 =  this.node.convertToNodeSpaceAR(pos);
                for(let i in this.item){
                    cc.tween(this.item[i]).by(dt,{
                        position : cc.v2(-pos2.x,0)
                    }).start();
                }
            }
            return;
        }
        this.isCheck = false;
        if(this._moveSpeed){
            let arr  = [];
            for(let i in this.item){
                arr.push(this.item[i].scale);
            }
            let index = this.getMaxIndex(arr);
            this.btnNode.active = true;
            let spk = this.item[index].getChildByName("demoMan").getComponent(sp.Skeleton);
            if(this.clickStatus[index] == 0){
                spk.setSkin("pf01");
                this.rightList[0].active = true;
                this.rightList[1].active = false;
                this.seeVideoBtn.active = false;
                for(let i in this.getTimeLab){
                    this.getTimeLab[i].active = false;
                }
                this.pieceList.active = false;

                if(gameData.getPetProcessSkin()[index] == 1){
                    this.eggBtn.active = false;
                    this.videoBtn.active = false;
                    this.exchangeNode.active = false;
                    if(!this.item[index].getChildByName("use").active){
                        this.btnNode.getChildByName("bg1").active = true;
                        this.btnNode.getChildByName("bg2").active = false;
                        this.btnNode.getChildByName("bg3").active = false;
                    }else{
                        this.btnNode.getChildByName("bg1").active = false;
                        this.btnNode.getChildByName("bg2").active = true;
                        this.btnNode.getChildByName("bg3").active = false;
                    }
                    if(index == 26){
                        this.pieceList.active = true;
                        for(let i = 0 ; i < this.pieceList.children.length ; i++){
                            if(i == 2){
                                this.pieceList.children[i].active = true;
                                if(gameData.colorfulPiece[9][0] == 1){
                                    this.pieceList.children[i].children[0].active = true;
                                }else{
                                    this.pieceList.children[i].children[0].active = false;
                                }
                            }else{
                                this.pieceList.children[i].active = false;
                                this.pieceList.children[i].children[0].active = false;
                            }
                        }
                    }else if(index == 28){
                        this.pieceList.active = true;
                        for(let i = 0 ; i < this.pieceList.children.length ; i++){
                            if(i == 1){
                                this.pieceList.children[i].active = true;
                                if(gameData.colorfulPiece[7][0] == 1){
                                    this.pieceList.children[i].children[0].active = true;
                                }else{
                                    this.pieceList.children[i].children[0].active = false;
                                }
                            }else{
                                this.pieceList.children[i].active = false;
                                this.pieceList.children[i].children[0].active = false;
                            }
                        }
                    }else if(index == 29){
                        this.pieceList.active = true;
                        for(let i = 0 ; i < this.pieceList.children.length ; i++){
                            if(i == 0){
                                this.pieceList.children[i].active = true;
                                if(gameData.colorfulPiece[8][0] == 1){
                                    this.pieceList.children[i].children[0].active = true;
                                }else{
                                    this.pieceList.children[i].children[0].active = false;
                                }
                            }else{
                                this.pieceList.children[i].active = false;
                                this.pieceList.children[i].children[0].active = false;
                            }
                        }
                    }
                }else{
                    if(index == 30){
                        this.eggBtn.active = true;
                        this.videoBtn.active = false;
                        this.exchangeNode.active = false;
                    } else if(index == 27){
                        this.eggBtn.active = false;
                        this.videoBtn.active = false;
                        this.exchangeNode.active = true;
                        //时间判断宠物小抖
                        gameData.judgeTimeCome(15,()=>{
                            this.getTimeLab[1].active = false;
                        },()=>{
                            this.getTimeLab[1].active = true;
                        });
                    }else if(index == 24){
                        this.exchangeNode.active = false;
                        if(gameData.getRoleProcessSkin()[20] == 1){
                            this.eggBtn.active = false;
                            this.videoBtn.active = true;
                        }else{
                            this.eggBtn.active = true;
                            this.videoBtn.active = false;
                        }
                    }else if((index > 18 && index < 24) || index == 27){
                        this.eggBtn.active = false;
                        this.videoBtn.active = false;
                        this.exchangeNode.active = true;
                    }else if(index == 18){
                        this.exchangeNode.active = false;
                        if(gameData.getRoleProcessSkin()[17] == 1){
                            this.eggBtn.active = false;
                            this.videoBtn.active = true;
                        }else{
                            this.eggBtn.active = true;
                            this.videoBtn.active = false;
                        }
                    }else if(index == 26){
                        this.exchangeNode.active = false;
                        this.pieceList.active = true;
                        for(let i = 0 ; i < this.pieceList.children.length ; i++){
                            if(i == 2){
                                this.pieceList.children[i].active = true;
                                if(gameData.colorfulPiece[9][0] == 1){
                                    this.pieceList.children[i].children[0].active = true;
                                }else{
                                    this.pieceList.children[i].children[0].active = false;
                                }
                            }else{
                                this.pieceList.children[i].active = false;
                                this.pieceList.children[i].children[0].active = false;
                            }
                        }

                        //熊猫崽崽
                        gameData.judgeTimeCome(14,()=>{
                            this.getTimeLab[2].active = false;
                            if(gameData.colorfulPiece[9][0] == 1){
                                this.eggBtn.active = false;
                                this.videoBtn.active = true;
                            }else{
                                this.eggBtn.active = true;
                                this.videoBtn.active = false;
                            }
                        },()=>{
                            this.getTimeLab[2].active = true;
                            this.eggBtn.active = true;
                            this.videoBtn.active = false;
                        });
                    }else if(index == 28){
                        this.exchangeNode.active = false;

                        this.pieceList.active = true;
                        for(let i = 0 ; i < this.pieceList.children.length ; i++){
                            if(i == 1){
                                this.pieceList.children[i].active = true;
                                if(gameData.colorfulPiece[7][0] == 1){
                                    this.pieceList.children[i].children[0].active = true;
                                }else{
                                    this.pieceList.children[i].children[0].active = false;
                                }
                            }else{
                                this.pieceList.children[i].active = false;
                                this.pieceList.children[i].children[0].active = false;
                            }
                        }

                        //舞狮
                        gameData.judgeTimeCome(12,()=>{
                            this.getTimeLab[3].active = false;
                            if(gameData.colorfulPiece[7][0] == 1){
                                this.eggBtn.active = false;
                                this.videoBtn.active = true;
                            }else{
                                this.eggBtn.active = true;
                                this.videoBtn.active = false;
                            }
                        },()=>{
                            this.getTimeLab[3].active = true;
                            this.eggBtn.active = true;
                            this.videoBtn.active = false;
                        });
                    }else if(index == 29){
                        this.exchangeNode.active = false;

                        this.pieceList.active = true;
                        for(let i = 0 ; i < this.pieceList.children.length ; i++){
                            if(i == 0){
                                this.pieceList.children[i].active = true;
                                if(gameData.colorfulPiece[8][0] == 1){
                                    this.pieceList.children[i].children[0].active = true;
                                }else{
                                    this.pieceList.children[i].children[0].active = false;
                                }
                            }else{
                                this.pieceList.children[i].active = false;
                                this.pieceList.children[i].children[0].active = false;
                            }
                        }

                        //福星小牛
                        gameData.judgeTimeCome(13,()=>{
                            this.getTimeLab[4].active = false;
                            if(gameData.colorfulPiece[8][0] == 1){
                                this.eggBtn.active = false;
                                this.videoBtn.active = true;
                            }else{
                                this.eggBtn.active = true;
                                this.videoBtn.active = false;
                            }
                        },()=>{
                            this.getTimeLab[4].active = true;
                            this.eggBtn.active = true;
                            this.videoBtn.active = false;
                        });
                    }else{
                        this.exchangeNode.active = false;
                        this.eggBtn.active = false;
                        this.videoBtn.active = false;
                    }
                    this.btnNode.getChildByName("bg1").active = false;
                    this.btnNode.getChildByName("bg2").active = false;
                    this.btnNode.getChildByName("bg3").active = true;
                }
            }else{
                spk.setSkin("pf02");
                this.rightList[0].active = false;
                this.rightList[1].active = true;
                this.pieceList.active = false;
                this.exchangeNode.active = false;
                this.eggBtn.active = false;
                this.videoBtn.active = false;
                for(let i in this.getTimeLab){
                    this.getTimeLab[i].active = false;
                }
                if(gameData.newYearPetLockStatus[index] == 1){
                    this.item[index].getChildByName("lock").active = false;
                    this.seeVideoBtn.active = false;
                    this.getTimeLab[0].active = false;
                    if(gameData.newYearPetUseStatus[index] == 1){
                        this.item[index].getChildByName("use").active = true;
                        this.btnNode.getChildByName("bg1").active = false;
                        this.btnNode.getChildByName("bg2").active = true;
                        this.btnNode.getChildByName("bg3").active = false;
                    }else{
                        this.item[index].getChildByName("use").active = false;
                        this.btnNode.getChildByName("bg1").active = true;
                        this.btnNode.getChildByName("bg2").active = false;
                        this.btnNode.getChildByName("bg3").active = false;
                    }
                }else{
                    this.seeVideoBtn.active = true;

                    //时间判断新年皮肤
                    gameData.judgeTimeCome(10,()=>{
                        this.getTimeLab[0].active = false;
                    },()=>{
                        this.getTimeLab[0].active = true;
                    });
                }
            }

        }
        for (let i = 0; i < this.item.length; i++) {

            if (this.Direction == 0) {
                this.item[i].x -= this._moveSpeed * dt * this.speed;
            } else {
                this.item[i].y -= this._moveSpeed * dt * this.speed;
            }
        }
        if (this._moveSpeed > 0) {
            this._moveSpeed -= dt * this.rub;
            if (this._moveSpeed < 0) {
                this._moveSpeed = 0;
            }
        } else {
            this._moveSpeed += dt * this.rub;
            if (this._moveSpeed > 0) {
                this._moveSpeed = 0;
            }
        }
        let moveTo = -this._moveSpeed * dt * this.speed;
        this.itemMoveBy(cc.v2(moveTo, moveTo));
        this.updatePos();
    }
}
